<?php
namespace app\admin\controller;

class Member extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'Member';
    }
    //
    protected function _before_index(){
        $thead = [
            'mid' => 'ID',
            'nickname' => '员工名称',
            'mgid' => '员工分组',
            'isrp' => '参与报数',
            'desc' => '备注信息',
            'used' => '使用状态',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
        //
        $condition = [];
        $condition['used'] = 1;
        $mgArr = model('MemberGroup')->where($condition)->field('mgid,gname')->select()->toArray();
        $this->assign('mgArr', $mgArr);
    }
    //
    protected function _filter($map){
        return $map;
    }
    //
    protected function _after_list($list){
        $mgArr = model('MemberGroup')->column('gname', 'mgid');
        foreach($list as $key => $row){
            $list[$key]['mgid'] = $mgArr[$row['mgid']] ?? '';
            $list[$key]['isrp'] = $row['isrp'] == '0' ? '否' : '是';
            $list[$key]['used'] = $row['used'] == '0' ? '未用' : '在用';
            $list[$key]['addtime'] = date('Y-m-d H:i:s', $row['addtime']);
        }
        return $list;
    }
    //
    protected function _before_add(){
        $condition = [];
        $condition['used'] = 1;
        $mgArr = model('MemberGroup')->where($condition)->select()->toArray();
        $this->assign('mgArr', $mgArr);
    }
    //
    protected function _before_insert($data){
        $data['addtime'] = time();
        return $data;
    }
    //
    protected function _after_add($data, $id){
        $this->log(['pk' => 'mid', 'value' => $id], $data);
    }
    //
    protected function _before_edit(){
        $this->_before_add();
    }
    //
    protected function _filter_edit($list){
        return $list;
    }
    //
    protected function _before_save($data){
        return $data;
    }
    //
    protected function _after_edit($data){
        $this->log(['pk' => 'mid', 'value' => $data['mid']], $data);
    }
    //
    protected function _filter_del(){
        return [1];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'mid', 'value' => $row['mid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}